﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.Standard;
using RoadTeam.BLL;
using System.Data;
using System.Web.Script.Serialization;
using RoadTeam.Pub;
using System.IO;
using RoadTeam.Enums;

namespace RoadTeam.Pase
{
    public class PaseMaintance:BasePase
    {
        #region 获取维修项目信息[GetMaintanceProjectSync]
        /// <summary>
        /// 根据机构标识获取维修项目信息
        /// 参数[OrganIdentifier]:机构标识
        /// 2013年11月4日11:02:33
        /// </summary>
        /// <param name="OrganIdentifier">机构标识</param>
        /// <returns>维修项目数据集合</returns>
        public static CsMaintanceProject GetMaintanceProjectSync(String OrganIdentifier)
        {
            BllBase bllbase = new BllBase();
            //bllbase.InserLog("方法名[GetMaintanceProjectSync]调用时间[" + DateTime.Now.ToString() + "]参数为:[" + OrganIdentifier + "]");

            bllbase.InsertLog(@"方法名[GetMaintanceProjectSync]调用时间[" + DateTime.Now.ToString() + "]参数为:[" + OrganIdentifier + "]",
@"RoadTeam.Pase.PaseMaintance", "GetMaintanceProjectSync(String OrganIdentifier)");


            CsMaintanceProjectBll bll = new CsMaintanceProjectBll();
            DataTable dt = bll.GetMaintanceProjectSync(OrganIdentifier);
            List<MaintanceProject> li = DtToList<MaintanceProject>(dt);
            CsMaintanceProject projects = new CsMaintanceProject();
            projects.Item = li.ToArray();
            return projects;
        }
        #endregion

        #region 插入维修单以及维修项目[InSertMaintenance]
        /// <summary>
        ///  插入维修单以及维修项目
        ///  参数[Json]:维修单以及维修项目Json集合
        /// </summary>
        /// <param name="Json">维修单以及维修项目Json集合</param>
        /// <returns>维修单以及维修项目并且生成唯一标识</returns>
        public static CsMaintenance InSertMaintenance(string Json)
        {
            BllBase bllbase = new BllBase();
           // bllbase.InserLog("方法名[InSertMaintenance]调用时间[" + DateTime.Now.ToString() + "]参数为:[" + Json + "]");

            bllbase.InsertLog("方法名[InSertMaintenance]调用时间[" + DateTime.Now.ToString() + "]参数为:[" + Json + "]",
@"RoadTeam.Pase.PaseMaintance", @"InSertMaintenance");

            JavaScriptSerializer jvs = new JavaScriptSerializer();
            CsMaintenance main = jvs.Deserialize<CsMaintenance>(Json);
            CsMaintenanceBll Main_bll = new CsMaintenanceBll();
            CsMaintenanceDetailBll Detail_bll = new CsMaintenanceDetailBll();
            DataTable Table_MainInfo = Main_bll.GetEmptyTable();
            DataRow dr_MainInfo;
            DataTable Table_DetailInfo = Detail_bll.GetEmptyTable();
            DataRow dr_DetailInfo;
            String Str = String.Empty;
            String DetailIdentifier = String.Empty;
            foreach (Maintenance item in main.Item)
            {
                Str = RoadTeam.Pub.CreateIdentifier.GetIdentifier();
                dr_MainInfo = Table_MainInfo.NewRow();
                dr_MainInfo["MaintenanceAddress"] = item.MainInfo.MaintenanceAddress;
                dr_MainInfo["MaintenanceCode"] = item.MainInfo.MaintenanceCode;
                dr_MainInfo["MaintenanceContact"] = item.MainInfo.MaintenanceContact;
                dr_MainInfo["MaintenanceContactPhone"] = item.MainInfo.MaintenanceContactPhone;
                dr_MainInfo["MaintenanceContent"] = item.MainInfo.MaintenanceContent;
                dr_MainInfo["MaintenanceCreater"] = item.MainInfo.MaintenanceCreater;
                dr_MainInfo["MaintenanceDate"] = item.MainInfo.MaintenanceDate;
                dr_MainInfo["MaintenanceIdentities"] = Str;
                item.MainInfo.MaintenanceIdentities = Str;
                dr_MainInfo["MaintenanceRemark"] = item.MainInfo.MaintenanceRemark;
                dr_MainInfo["MaintenanceType"] = item.MainInfo.MaintenanceType;
                dr_MainInfo["OrganIdentifier"] = item.MainInfo.OrganIdentifier;
                dr_MainInfo["UnitIdentifier"] = item.MainInfo.UnitIdentifier;
                Table_MainInfo.Rows.Add(dr_MainInfo);
                foreach (Maintenance_DetailInfo detail in item.DetailInfo)
                {
                    DetailIdentifier = RoadTeam.Pub.CreateIdentifier.GetIdentifier();
                    dr_DetailInfo = Table_DetailInfo.NewRow();
                    dr_DetailInfo["CreateDateTime"] = detail.CreateDateTime;
                    dr_DetailInfo["MainDetailIdentifier"] = DetailIdentifier;
                    detail.MainDetailIdentifier = DetailIdentifier;
                    dr_DetailInfo["MainIdentifier"] = Str;
                    detail.MainIdentifier = Str;
                    dr_DetailInfo["OrganIdentifier"] = detail.OrganIdentifier;
                    dr_DetailInfo["ProjectIdentitfier"] = detail.ProjectIdentitfier;
                    Table_DetailInfo.Rows.Add(dr_DetailInfo);
                }
            }
            Main_bll.InSertMaintenance(Table_MainInfo);
            Detail_bll.InsertMaintenanceDetail(Table_MainInfo, Table_DetailInfo);
            return main;
        }
        #endregion

        #region 插入维修单中图片[InSertMaintenancePic]

        /// <summary>
        /// 插入维修单相关图片
        /// 参数[Json]:图片信息
        /// 参数[Img]:转换成64位字符串后的图片
        /// 创建时间:2013年11月4日11:15:18
        /// </summary>
        /// <param name="Json">图片信息</param>
        /// <param name="Img">转换成64位字符串后的图片</param>
        /// <returns>是否上传成功</returns>
        public static bool InSertMaintenancePic(String Json, String Img)
        {
            BllBase bllbase = new BllBase();
           //blbase.InserLog(@"调用方法[InSertMaintenancePic]调用时间[" + DateTime.Now.ToString() + "],参数Json为:" + Json + "参数Img为:" + Img);

            bllbase.InsertLog(@"方法名[InSertMaintenancePic]调用时间[" + DateTime.Now.ToString() + "]参数为:[" + Json + "][" + Img + "]",
@"RoadTeam.Pase.PaseMaintance", "InSertMaintenancePic(String Json, String Img)");

            byte[] ImageByte = Convert.FromBase64String(Img);
            JavaScriptSerializer jvs = new JavaScriptSerializer();
            RoadTeam.Standard.CsMaintenanceFile fileInfo = jvs.Deserialize<RoadTeam.Standard.CsMaintenanceFile>(Json);
            String RtSysPath = new RtParameter(RoadTeam.Enums.ParaName.AppFilePath).ParamValue;
            String PathDate = DateTime.Now.Year.ToString() + ((DateTime.Now.Month.ToString().Length == 2) ? DateTime.Now.Month.ToString() : "0" +
              DateTime.Now.Month.ToString()) + DateTime.Now.Day.ToString();
            String Paths = GlobalConfig.FilePath + "\\" + PathDate;
            RtSysPath = RtSysPath + "/" + PathDate;
            if (!Directory.Exists(Paths + "\\" + fileInfo.StaffName))//判断是否存在        
                Directory.CreateDirectory(Paths + "\\" + fileInfo.StaffName);//创建新路径
            Paths = Paths + @"\" + fileInfo.StaffName;
            RtSysPath = RtSysPath + @"/" + fileInfo.StaffName;
            try
            {
                using (FileStream fs = new FileStream(Paths + "\\" + fileInfo.ImgName, FileMode.Create))
                {
                    fs.Write(ImageByte, 0, ImageByte.Length);
                    fs.Flush();
                    fs.Close();
                    fs.Dispose();
                }
            }
            catch
            {
                return false;
            }
            CsMaintenanceFileBll bll = new CsMaintenanceFileBll();
            bool flag = (FileOperate.FileExist(Paths, fileInfo.ImgName)
                && bll.InsertFileInfo(fileInfo.BusninessIdentitfier, Paths, RtSysPath, fileInfo.ImgName, fileInfo.ImgType, fileInfo.BusninessType));
            return flag;
        }
        #endregion

    }
}
